package cn.dansj.common.utils.lang;

import cn.dansj.common.utils.transfer.ArrayUtils;
import cn.dansj.common.utils.transfer.DefaultMethods;

import java.util.List;
import java.util.stream.Collectors;

public class PairGroup<K, V> extends DefaultMethods {
    private final String groupName;
    private final List<Pair<K, V>> pairs;

    private PairGroup(String groupName, List<Pair<K, V>> pairs) {
        this.groupName = groupName;
        this.pairs = pairs;
    }

    public static <K, V> PairGroup<K, V> of(String groupName, List<Pair<K, V>> pairs) {
        return new PairGroup<>(groupName, pairs);
    }

    @SafeVarargs
    public static <K, V> PairGroup<K, V> of(String groupName, Pair<K, V>... pairs) {
        return new PairGroup<>(groupName, ArrayUtils.asList(pairs));
    }

    public PairGroup<K, V> add(K key, V value) {
        pairs.add(Pair.of(key, value));
        return this;
    }

    public PairGroup<K, V> add(Pair<K, V> pair) {
        pairs.add(pair);
        return this;
    }

    public PairGroup<V, K> reverse() {
        return new PairGroup<>(groupName, pairs.stream().map(Pair::reverse).collect(Collectors.toList()));
    }

    public List<Pair<K, V>> getPairs() {
        return pairs;
    }

    public String getGroupName() {
        return groupName;
    }

    @Override
    public String toString() {
        return "PairGroup (" + groupName + ") " + pairs;
    }
}
